package com.glacier.yuyuan.modules.admin.sys.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.common.web.BaseController;
import com.glacier.yuyuan.modules.admin.sys.domain.Post;
import com.glacier.yuyuan.modules.admin.sys.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2022-11-10 15:12
 * 岗位控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("${yuyuan.adminPath}/sys/post")
@Controller
public class PostController extends BaseController {

    private PostService postService;

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @GetMapping("")
    public String list() {
        return "admin/sys/post/postList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<Post>> page(Page<Post> page, Post post) {
        page = postService.findPage(page, post);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(Post post, Model model) {
        if (StrUtil.isNotBlank(post.getId())) {
            post = postService.findById(post.getId());
        }
        model.addAttribute("entity", post);
        return "admin/sys/post/postForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(Post post) {
        return Result.ok("操作成功！", postService.save(post));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        postService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
